package br.com.nooclix.ptt.ws.application;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import br.com.nooclix.ptt.core.application.BusinessSpaceApplication;
import br.com.nooclix.ptt.core.domain.space.BusinessSpace;
import br.com.nooclix.ptt.core.infrastructure.util.ApplicationContextRegisterCore;
import br.com.nooclix.ptt.core.infrastructure.util.ResponseData;
import br.com.nooclix.ptt.ws.adapter.Adapter;
import br.com.nooclix.ptt.ws.adapter.impl.BusinessSpaceAdatpter;

@Path("/space")
public class BusinessSpaceRestApplication {

	private Adapter<BusinessSpace> adapter;
	private BusinessSpaceApplication spaceApplication;

	public BusinessSpaceRestApplication() {
		super();
		adapter = new BusinessSpaceAdatpter();
		spaceApplication = ApplicationContextRegisterCore.getBean(
				"businessSpaceApplication", BusinessSpaceApplication.class);
	}

	@GET
	@Path("/find/name/{name}/{offset}/{maxResult}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response findByName(@PathParam("name") String name,
			@PathParam("offset") int offset,
			@PathParam("maxResult") int maxResult) {

		Response response = null;
		if (name.equalsIgnoreCase("null")) {
			name = null;
		}
		ResponseData<BusinessSpace> rp = spaceApplication.findByCriterions(
				name, offset, maxResult);
		String json = adapter.convert(rp);
		response = Response.ok(json).build();
		return response;
	}
}
